<template>
    <view class="u-demo-block">
        <view class="addressView" @click="goPage('/pages/sjh/mail')">
            <view class="av-1">{{mailDefault.address}}</view>
            <view class="av-2">
                <span class="av-span1">{{mailDefault.addressee}}</span>
                <span class="av-span2">{{mailDefault.phone}}</span>
            </view>
        </view>


        <view class="swipe-action u-border-top u-border-bottom" v-for="(cart,index) in cartDto">
            <view class="swipe-action__content">
                <view>
                    <img :src="cart.picUrl" style="width: 100px">
                </view>
                <view>
                    <p>{{cart.spuName}}</p>
                    <p style="color: #B2B2B2;font-size: 9px">{{cart.skuName}}</p>
                </view>
                <view>
                    <span>￥{{cart.price}}</span>
                </view>
                <view>
                    数量: {{cart.amount}}
                </view>
            </view>
        </view >
        <view class="fixedView">
            <view>￥{{totalPrice}}</view>
            <button @click="createGoodsOrder">去支付</button>
        </view>
    </view>
</template>
<script>
    import Request from '../../utils/request'
    export default {
        data() {
            return{
                totalPrice:'',
                mailDefault:{

                },
                cart:"",
                userId:1,
                cartDto:[]
            }
        },
        created(){
            this.getMailDefault()
            //uni.removeStorageSync("cart")
            let NoCart=uni.getStorageSync("cart")
            let cart=uni.getStorageSync("Iscart")
            //alert(cart)
            if(cart!=""){
                //alert("走购物车")
                this.seeCart()
            }else{
                this.cart=NoCart
                this.directPay(NoCart)
            }
            //如果是购物车跳转来的，就查询当前用户的购物车列表
            //alert(JSON.stringify(cart))
        },
        methods:{
            /*跳转页面*/
            goPage(url){
                //alert(this.cart)
                if(this.cart=="") {
                    uni.setStorageSync("Iscart",true)
                }
                if(this.cart!="") {
                    uni.setStorageSync("cart", this.cart)
                }
                uni.setStorageSync("fromOrder",true)
                uni.navigateTo({
                    url: url
                });
            },
            /*获取下单地址*/
            getMailDefault(){
                let mailId=uni.getStorageSync("mailId")
                if(mailId==undefined || mailId==""){
                    Request.request('management-mall/leyou.order/order-mail/getMailDefault'
                        , {userId: this.userId}, 'post').then(res => {
                        console.log(res)
                        // console.log(res.data.specsList)
                        this.mailDefault=res.data.one
                    }).catch(error => {
                        console.error(error)
                    })
                }else{
                    uni.removeStorageSync("mailId")
                    Request.request('management-mall/leyou.order/order-mail/getMailById'
                        , {mailId: mailId}, 'post').then(res => {
                        console.log(res)
                        // console.log(res.data.specsList)
                        this.mailDefault=res.data.one
                    }).catch(error => {
                        console.error(error)
                    })
                }

            },
            /*购物车*/
            seeCart(){
                Request.request('management-mall/leyou.order/order-shopping-cart/seeCart'
                    ,{userId:this.userId},'post').then(res=>{
                    console.log(res)
                    this.cartDto=res.data.list
                    this.totalPrice=res.data.totalPrice
                    uni.removeStorageSync("Iscart")
                }).catch(error=>{
                    console.error(error)
                })
            },
            /*直接购买*/
            directPay(cart){
                if(cart!="" && cart!=undefined) {
                    Request.request('management-mall/leyou.order/order-shopping-cart/directPay'
                        , cart, 'post').then(res => {
                        console.log(res)
                        this.cartDto = res.data.list
                        this.totalPrice = res.data.totalPrice
                    }).catch(error => {
                        console.error(error)
                    })
                }
            },
            /*下单*/
            createGoodsOrder(){
                let json=[]
                for(let i=0;i<this.cartDto.length;i++){
                    let j={
                        cartId:this.cartDto[i].id,
                        skuId:this.cartDto[i].skuId,
                        spuId:this.cartDto[i].spuId,
                        amount:this.cartDto[i].amount,
                        userId:this.userId,
                        mailId:this.mailDefault.id
                    };
                    json.push(j)
                }
                //alert(JSON.stringify(json))
                Request.requestForJSON('management-mall/leyou.order/order-goods/createGoodsOrder'
                    ,JSON.stringify(json),'post').then(res=>{
                    console.log(res)
                    if(res.success){
                        let orders=res.data.orders
                        uni.setStorageSync("orders",orders)
                        uni.navigateTo({
                            url: '/pages/sjh/payment'
                        });
                    }
                }).catch(error=>{
                    console.error(error)
                })
            }
        }
    }
</script>

<style>
    .addressView{
        background: #e1f3d8;
        padding: 10px;
        margin: 5px;
    }
    .av-1{

    }
    .av-2{
        color: #B2B2B2;
    }
    .av-span1{
        margin-right: 20px;
    }
    .av-span2{

    }
    .fixedView{
        width: 100%;
        padding: 15px 10px;
        position: fixed;
        bottom: 0;
        background: #e1f3d8;
    }
    .fixedView>view{
        padding-top: 10px;
        float: left;
    }
    .fixedView>button{
        float: right;
        margin-right: 35px;
    }
    .swipe-action__content>view{
        margin-left: 10px;
    }
</style>
